<?php
/**
 * Created by IntelliJ IDEA.
 * User: ahwwl
 * Date: 17-8-9
 * Time: 下午3:38
 */

namespace app\controller;


use think\Request;

/**
 * Class Sysmenu
 * @property \app\model\AdminMenu AdminMenu
 * @package app\controller
 */
class Adminmenu extends CommonController
{
    /**
     * Sysmenu constructor.
     * @param Request|null $request
     */
    function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->AdminMenu = new \app\model\AdminMenu();
    }

    /**
     * @return mixed
     */
    function tree()
    {
        $this->assign("page_title", "系统菜单");
        return $this->fetch();
    }
    function ajax_sys_menu_list()
    {
        try
        {
            $page = input("page",1,"intval");
            $rows = input("rows",30,"intval");
            $where = [];

            $count = $this->AdminMenu->where($where)->count();

            $data_list = $this->AdminMenu->where($where)->order("sort asc,id asc")->select();
            $data_list = list_to_tree($data_list);
            $this->resultdata["data"] = $data_list;
            $this->resultdata["error"] = 0;
            $this->resultdata["total"] = intval($count);
            $this->resultdata["page"] = intval($page);
            $this->resultdata["rows"] = intval($rows);
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_sys_menu_info()
    {
        try
        {
            $id = input("id",0,"intval");
            if(empty($id))
                $this->E("参数不能为空");
            $info = $this->AdminMenu->where(["id"=>$id])->find();

            $this->resultdata["data"] = $info;
            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_sys_menu_save()
    {
        try {

            $id = input("id", 0, "intval");
            $name = input("name", "", "trim");
            $pid = input("pid", 0, "intval");
            $link = input("link", "", "trim");
            $icon = input("icon", "", "trim");
            $sort = input("sort", 0, "intval");

            if (empty($name)) {
                $this->E("参数不正确");
            }
            $data = [

                "name" => $name,
                "pid" => $pid,
                "link"=>$link,
                "icon" => $icon,
                "sort" => $sort,

            ];

            if (empty($id)) {
                $user_info = $this->AdminMenu->where(["name" => $name])->find();
                if (!empty($user_info))
                    $this->E("菜单己存在");
                $query = $this->AdminMenu->create($data);
            } else {
                $query = $this->AdminMenu->where(['id' => $id])->update($data);
            }

            if ($query)
                $this->resultdata["error"] = 0;
            else
                $this->resultdata["msg"] = "失败";
        } catch (\Exception $e) {
            $this->resultdata["msg"] = $e->getMessage();
        }
        echo json_encode($this->resultdata);
    }
    function ajax_sys_menu_del()
    {
        try {


            $row_list = input("row_list", []);

            if (empty($row_list) ) {
                $this->E("参数不正确");
            }
            $id_list = make_array($row_list,"id");
            if (empty($id_list) ) {
                $this->E("参数不正确");
            }


            $query = $this->AdminMenu->where(['id' => ["in",$id_list]])->delete();

            if ($query)
                $this->resultdata["error"] = 0;
            else
                $this->resultdata["msg"] = "删除失败";
        } catch (\Exception $e) {

            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_set_sys_menu_is_log()
    {
        try {


            $id = input("id", 0,"intval");
            $is_log = input("is_log", 0,"intval");

            if (empty($id) ||!in_array($is_log,[0,1])) {
                $this->E("参数不正确");
            }

            $query = $this->AdminMenu->where(['id' => $id])->update(["is_log"=>$is_log]);

            if ($query)
                $this->resultdata["error"] = 0;
            else
                $this->resultdata["msg"] = "删除失败";
        } catch (\Exception $e) {

            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
}